import { useState } from 'react'

interface ReturnProps<T> {
  value: T[]
  removeIndex: (index: number) => void
  add: (val: T) => void
  clear: () => void
}

export const useArray = <T>(arr: T[]): ReturnProps<T> => {
  const [value, setValue] = useState(arr)
  const removeIndex = (index: number) => {
    const copy = [...value]
    copy.splice(index, 1)
    setValue(copy)
  }
  const add = (val: T) => {
    setValue([...value, val])
  }
  const clear = () => {
    setValue([])
  }
  return {
    value,
    removeIndex,
    add,
    clear,
  }
}
